import nltk
from rai.sem.FOL import *
import test.file

import unittest
class TestMace(unittest.TestCase):
    
    def testMace(self):

        #lp = nltk.LogicParser()
        #a3 = lp.parse('exists x.(man(x) & walks(x))')
        #c1 = lp.parse('mortal(socrates)')
        #c2 = lp.parse('-mortal(socrates)')
        #mb = nltk.Mace(5)
        #print mb.build_model(None, [a3, c1])
        #print mb.build_model(None, [a3, c2])
        #print mb.build_model(None, [c1, c2])
        
        __dir__ = test.file.__path__
        grammar = "file:" + __dir__[0] + "\\org.event.fcfg"
        #inputs = ["Joe chases Fido", "Fido is a dog", "Every dog barks"]
        inputs = ["Suzie loves John", "Suzie walks"] #, "Suzie is in Noosa"] #, "Jim is a boy", "Jim loves the kitchen", "Suzie loves the kitchen", "John performs Suzie", "Alice loves Bob"]
        #questions = ["Joe chases a dog"]
        #questions = ["Who chases a dog"]
        questions = ["Suzie loves John"]
        #questions = ["Fido barks"]
        
        semreps = nltk.sem.batch_interpret(inputs, grammar)
        model = []
        for semrep in semreps:
            for (syn, sem) in semrep:
                model.append(sem)
        
        lp = nltk.LogicParser()
        #model.append(lp.parse("-(Joe = Fido)"))
        #model.append(lp.parse("-(Fido = Mia)"))
        #model.append(lp.parse("-(Joe = Mia)"))
        
        semreps = nltk.sem.batch_interpret(questions, grammar)
        countermodel = []
        for semrep in semreps:
            for (syn, sem) in semrep:
                countermodel.append(sem)
        
        #q = lp.parse(str(countermodel[0]) + "(Mia)")
        #q = lp.parse("-(" + str(countermodel[0]) + ")")
        #q = lp.parse("all x. -((dog(z) & chase(x,z)))")
        q = countermodel[0].simplify()
        #q = q.simplify()#.negate()
        print model
        print [q]
        print "START MACE"
        mc = nltk.MaceCommand(q, assumptions=model)
        print mc.build_model()
        print mc.valuation
        
        #prover = nltk.Prover9()
        #prover.prove(q, model)
        
        #modelBuilder = nltk.Mace(5)
        #print modelBuilder.build_model(None, model)

if __name__ == "__main__":
    unittest.main()